package net.hn.hnms.biz.cockpit.utils;


import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.hn.hnms.sys.common.core.utils.DateUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author mkq
 * @Date 2022-08-25 18:56
 * @Description
 */
public class OweDateUtils  {

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意三个参数的时间格式要一致
     *
     * @param nowTime
     * @param startTime
     * @param endTime
     * @return 在时间段内返回true，不在返回false
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }



    public static String getContinueTime(Date intoTime, Date outTime) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long diff = outTime.getTime() - intoTime.getTime();
            diff = diff < 0 ? 0 : diff;
            long day = diff / (24 * 60 * 60 * 1000);
            long hour = (diff / (60 * 60 * 1000) - day * 24);
            long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            if (s > 0) {
                min += 1;
            }
            if (day > 0) {
                return day + "天" + hour + "小时" + min + "分";
            } else if (hour > 0) {
                return hour + "小时" + min + "分";
            } else {
                return min + "分";
            }
        } catch (Exception e) {
            return "";
        }
    }

    public static double getContinualHour(Date startTime, Date endTime) {
        long times = endTime.getTime() - startTime.getTime();
        double hours = (double) times / (60 * 60 * 1000);
        BigDecimal a = BigDecimal.valueOf(hours);
        return a.doubleValue();
    }

    public static String addDateMinut(String day, int x, String mformat) {
        // 24小时制
        SimpleDateFormat format = new SimpleDateFormat(mformat);
        //引号里面个格式也可以是 HH:mm:ss或者HH:mm等等，很随意的，不过在主函数调用时，要和输入的变
        //量day格式一致
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, x);// 24小时制
        date = cal.getTime();
        cal = null;
        return format.format(date);

    }

    /**
     * 格式化时间
     *
     * @param date
     * @param format
     * @return
     */
    public static String dateformat(String date, String format) {
        SimpleDateFormat original = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sformat = new SimpleDateFormat(format);
        Date _date = null;
        try {
            _date = original.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sformat.format(_date);
    }

    public static final DateTimeFormatter DATE_TIME_FORMATTER_DAY = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static final DateTimeFormatter DATE_TIME_FORMATTER_HOURS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter DATE_TIME_FORMATTER_MY_HOURS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00");


    private static final DateTimeFormatter DATE_TIME_FORMATTER_M = DateTimeFormatter.ofPattern("yyyy-MM");

    private static final DateTimeFormatter DATE_TIME_FORMATTER_Y = DateTimeFormatter.ofPattern("yyyy");

    public static List<String> getXData(String beginDate, String endDate, String type) {
        List<String> result = new ArrayList<String>();

        switch (type) {
            case "hour":
                LocalDateTime startDateTime1 = LocalDateTime.parse(beginDate, DATE_TIME_FORMATTER_HOURS);
                LocalDateTime endDateTime1 = LocalDateTime.parse(endDate, DATE_TIME_FORMATTER_HOURS);
                while (!startDateTime1.isAfter(endDateTime1)) {
                    result.add(startDateTime1.format(DATE_TIME_FORMATTER_MY_HOURS));
                    startDateTime1 = ChronoUnit.HOURS.addTo(startDateTime1, 1);
                }
                break;
            case "day":
                LocalDate startDateTime = LocalDate.parse(beginDate, DATE_TIME_FORMATTER_DAY);
                LocalDate endDateTime = LocalDate.parse(endDate, DATE_TIME_FORMATTER_DAY);
                while (!startDateTime.isAfter(endDateTime)) {
                    result.add(startDateTime.format(DATE_TIME_FORMATTER_DAY));
                    startDateTime = ChronoUnit.DAYS.addTo(startDateTime, 1);
                }
                break;
            case "month":
                startDateTime = LocalDate.parse(beginDate, DATE_TIME_FORMATTER_DAY);
                endDateTime = LocalDate.parse(endDate, DATE_TIME_FORMATTER_DAY);
                while (!startDateTime.isAfter(endDateTime)) {
                    result.add(startDateTime.format(DATE_TIME_FORMATTER_M));
                    startDateTime = ChronoUnit.MONTHS.addTo(startDateTime, 1);
                }
                break;
            case "year":
                startDateTime = LocalDate.parse(beginDate, DATE_TIME_FORMATTER_DAY);
                endDateTime = LocalDate.parse(endDate, DATE_TIME_FORMATTER_DAY);
                while (!startDateTime.isAfter(endDateTime)) {
                    result.add(startDateTime.format(DATE_TIME_FORMATTER_Y));
                    startDateTime = ChronoUnit.YEARS.addTo(startDateTime, 1);
                }
                break;
            default:
                break;
        }
        return result;
    }

    public static boolean isOneYearApart(String beginDate, String endTime) {
        LocalDate time1 = LocalDate.parse(beginDate, DATE_TIME_FORMATTER_DAY);
        LocalDate time2 = LocalDate.parse(endTime, DATE_TIME_FORMATTER_DAY);
        long years = time1.until(time2, ChronoUnit.YEARS);
        return Math.abs(years) >= 1;
    }

}
